package com.readywu.btchecker;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

import com.androidquery.callback.AjaxStatus;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class MainActivity extends Activity {
    protected static final int MSG_GET_TASK_LIST = 0;
    ApiDataClient client;
    private Timer mTimer;
    private TimerTask timerTask;
    private Vibrator vibrator;
    private TextView lastTextView;
    private TextView buyTextView;
    private TextView sellTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lastTextView = (TextView) findViewById(R.id.last);
        buyTextView = (TextView) findViewById(R.id.buy);
        sellTextView = (TextView) findViewById(R.id.sell);
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        client = new ApiDataClient(this);
        mTimer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(MSG_GET_TASK_LIST);
            }
        };
        mTimer.schedule(timerTask, Constants.REFRESH_TIME,
                Constants.REFRESH_TIME);
        getTicker();
    }

    // Handler 消息机制 Message
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {

            case MSG_GET_TASK_LIST:
                getTicker();
                break;

            default:
                break;
            }
        }
    };

    private void getTicker() {
        client.get("https://data.btcchina.com/data/ticker", null,
                new ApiCallback<JSONObject>() {

                    @Override
                    public void callback(String url, JSONObject object,
                            AjaxStatus status) {
                        // TODO Auto-generated method stub
                        super.callback(url, object, status);
                        if (object != null) {
                            Log.d("ddd", object.toString());

                            Gson gson = new Gson();
                            try {
                                Ticker ticker = gson.fromJson(object
                                        .getJSONObject("ticker").toString(),
                                        Ticker.class);

                                lastTextView.setText(ticker.getLast());
                                buyTextView.setText(ticker.getBuy());
                                sellTextView.setText(ticker.getSell());
                                if (Float.valueOf(ticker.getLast()) <= 4500) {
                                    long[] pattern = { 100, 400, 100, 400 }; // 停止
                                                                             // 开启
                                                                             // 停止
                                                                             // 开启
                                    vibrator.vibrate(pattern, -1);
                                } else if (Float.valueOf(ticker.getLast()) > 6000) {
                                    Log.e("tag", "msg");
                                    long[] pattern = { 100, 300, 100, 300 }; // 停止
                                                                             // 开启
                                                                             // 停止
                                                                             // 开启
                                    vibrator.vibrate(pattern, -1);
                                }
                            } catch (JsonSyntaxException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }

                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timerTask.cancel();
        mTimer.cancel();
        vibrator.cancel();
    }
}
